#include "spinboxdelegate.h"

#include <QSpinBox>

spinBoxDelegate::spinBoxDelegate(QObject* parent) : QItemDelegate(parent) {}

// 生成一个 QSpinBox
QWidget *spinBoxDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QSpinBox* editor = new QSpinBox(parent);
    editor->setMinimum(0);
    editor->setMaximum(100);
    return editor;
}

// 把双击后我们输入的数值设置进 spinBox 里面去
void spinBoxDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
    int value = index.model()->data(index, Qt::EditRole).toInt();
    QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
    spinBox->setValue(value);
}

// 把设置进 spinBox 里面去的数值，设置进去 model 里面去
void spinBoxDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
    QSpinBox* spinBox = static_cast<QSpinBox*>(editor);
    spinBox->interpretText(); // 得使用的哦
    int value = spinBox->value();
    model->setData(index, value, Qt::EditRole);
}

// 双击的时候是得 createEditor 函数返回的 editor 在双击的位置上
void spinBoxDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    editor->setGeometry(option.rect);
}

